package com.unknown

import grails.converters.JSON
import grails.plugins.springsecurity.Secured

import com.unknown.domain.repository.NotFoundException
import com.unknown.world.Exit
import com.unknown.world.Room

@Secured(['ROLE_ADMIN'])
class ExitController {
	def roomService
	
	def create = {
		validate(params, 'id')
		validate(params, 'direction')
		validate(params, 'row')
		validate(params, 'col')
		
		Room room = roomService.createExit(params)
		def result = ['id':room.id.toString()]
		
		render result as JSON
	}
	
	def remove = {
		validate(params, 'id')
		validate(params, 'exit')
		
		Room room = roomService.findById( params.id );
		for(Exit exit : room.exits){
			if(exit.name == params.exit) room.exits.remove(exit)
		}		
		def result = ['id':room.id.toString()]
		
		render result as JSON
	}
	
	def validate(def params, String param){
		if(!params[param])
			throw new NotFoundException(param);
	}
}
